package org.vnl.platform.service.rule;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * 預設的規則儲存器
 * 
 * @author Vaughan
 *
 */
public class DefaultRuleConfigContainer implements IRuleConfigContainer {
    
    private HashMap<String,HashMap<String, List<IRuleConfig>>> pool;

    public DefaultRuleConfigContainer() {
        this.pool = new HashMap<String, HashMap<String,List<IRuleConfig>>>();
    }
    
    @Override
    public List<? extends IRuleConfig> getRuleConfigs(String owner, String ruleId) throws RuleException {
        if (this.pool == null) {
            return new LinkedList<IRuleConfig>();
        }
        HashMap<String,List<IRuleConfig>> o = this.pool.get(owner);
        if (o == null) {
            return new LinkedList<IRuleConfig>();
        }
        List<IRuleConfig> r = o.get(ruleId);
        if (r == null) {
            return new LinkedList<IRuleConfig>();
        }
        return r;
    }
    
    public void addRule(String owner, String ruleId, IRuleConfig config) {
        if (this.pool == null) {
            this.pool = new HashMap<String, HashMap<String,List<IRuleConfig>>>();
        }
        HashMap<String,List<IRuleConfig>> o = this.pool.get(owner);
        if (o == null) {
            o = new HashMap<String, List<IRuleConfig>>();
            this.pool.put(owner, o);
        }
        List<IRuleConfig> r = o.get(ruleId);
        if (r == null) {
            r = new LinkedList<IRuleConfig>();
            o.put(ruleId, r);
        }
        r.add(config);
    }

}
